package core.controller;

import core.pojo.Customer;
import core.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @ResponseBody
    @RequestMapping("json")
    public Customer json(Customer customer) {

        customer.setCust_name("小白");
        customer.setCust_pwd("123321");
        customer.setCust_phone("12345678910");
        customer.setCust_address("北京");
        customer.setCid(1);
        return customer;

    }

    @RequestMapping("showjson")
    public String showJson(Customer customer, HttpServletRequest request) {
        List<Customer> list = customerService.findCustomer();
        request.setAttribute("list", list);
        return "index";
    }

    @RequestMapping("deleteById")
    public String deleteByID(Integer cid) {

        customerService.deleteById(cid);
        return "redirect:showjson";
    }
    @RequestMapping("editById")
    public String getUser(int cid,HttpServletRequest request,Model model){
        model.addAttribute("customer", customerService.findById(cid));
        return "deitUser";
    }
    @RequestMapping("update")
    public String update(Customer customer){
        customerService.update(customer);
        return "redirect:showjson";
    }


    @RequestMapping("toAddCustomer")
    public String toAddCustomer() {
        return "addCustomer";
    }

    @RequestMapping("addCustomer")
    public String addCustomer(Customer customer) {
        customerService.save(customer);
        return "redirect:showjson";
    }




}
